package com.lwj.kotlin.remotelibrary

import android.Manifest
import android.app.Activity
import android.app.WallpaperManager
import android.content.Intent
import android.content.pm.PackageManager
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.lwj.kotlin.remotelibrary.ProxyTest.DynamicEatProxys
import com.lwj.kotlin.remotelibrary.ProxyTest.Human
import com.lwj.kotlin.remotelibrary.ProxyTest.HumanImpl
import com.lwj.kotlin.remotelibrary.annotation.Library_Annotation
import com.lwj.kotlin.remotelibrary.banner.BannerADImageLoader
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer
import kotlinx.android.synthetic.main.library_activity_main.*
import org.jetbrains.anko.startActivity
import java.lang.reflect.Proxy
import java.util.ArrayList

class Library_MainActivity : AppCompatActivity() {


    var videoLiveWallpaper: VideoLiveWallpaper? = null




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.library_activity_main)

        var images = ArrayList<String>()
        var titles = ArrayList<String>()
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537441132696&di=84c7d29ea12fb12ac0909a1bb993e7d5&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01d56b5542d8bc0000019ae98da289.jpg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1538035968&di=6a03ca3621bdfc4df65efc9982498815&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.90sjimg.com%2Fback_pic%2F00%2F00%2F69%2F40%2F54aac7e56ce94193542ad3d03d216d34.jpg")
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537441208874&di=b0a0f03a6e11b9ee13b5c63f0b999cd7&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F018f94592e27bba801216a3e7e4501.jpg")
        titles.add("共创辉煌")
        titles.add("进无止境")
        titles.add("以曲为美")
        //设置banner样式
        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE)
        //设置图片加载器
        banner.setImageLoader(BannerADImageLoader())
        //设置图片集合
        banner.setImages(images)
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage)
        //设置标题集合（当banner样式有显示title时）
        banner.setBannerTitles(titles)
        //设置自动轮播，默认为true
        banner.isAutoPlay(true)
        //设置轮播时间
        banner.setDelayTime(2000)
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER)
        //banner设置方法全部调用完毕时最后调用
        banner.start()

        //设置视频桌面
        tv_set_video.setOnClickListener {
            PictureSelector.create(this)
                .openGallery(PictureMimeType.ofVideo())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .maxSelectNum(9)// 最大图片选择数量
                .minSelectNum(1)// 最小选择数量
                .imageSpanCount(4)// 每行显示个数
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .previewImage(false)// 是否可预览图片
                .previewVideo(true)// 是否可预览视频
                .enablePreviewAudio(true) // 是否可播放音频
                .isCamera(true)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .enableCrop(false)// 是否裁剪
                .compress(true)// 是否压缩
                .synOrAsy(true)//同步true或异步false 压缩 默认同步
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                .isGif(false)// 是否显示gif图片
                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                .circleDimmedLayer(false)// 是否圆形裁剪
                .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .openClickSound(false)// 是否开启点击声音
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST)//结果回调onActivityResult code
        }

        //设置透明桌面
        tv_set_transparent.setOnClickListener {
            checkSelfPermission()
        }

        //动态代理测试

        tv_proxy.setOnClickListener {
            var humanImpl = HumanImpl()
            var dynamicEatProxy = DynamicEatProxys(humanImpl)
            var classLoader = humanImpl.javaClass.classLoader
            var interfaces = humanImpl.javaClass.interfaces
            var newProxyInstance = Proxy.newProxyInstance(classLoader, interfaces, dynamicEatProxy)
            var human = newProxyInstance as Human
            human.eat("饭")
            human.toilet()
        }

        //注解
        tv_annotation.setOnClickListener {
            startActivity<Library_Annotation>()
        }
    }


    private fun checkSelfPermission() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.CAMERA),
                110
            )
        } else {
            startWallpaper()
        }
    }

    /**
     * 选择壁纸
     */
    private fun startWallpaper() {
        val pickWallpaper = Intent(Intent.ACTION_SET_WALLPAPER)
        val chooser = Intent.createChooser(pickWallpaper, getString(R.string.choose_wallpaper))
        startActivity(chooser)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode === Activity.RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    val selectList = PictureSelector.obtainMultipleResult(data)
                    Config.mVideoName = selectList[0].path
                    try {
                        var instance = WallpaperManager.getInstance(this)
                        instance.clear()
                    } catch (e: Exception) {
                    }
                    videoLiveWallpaper = VideoLiveWallpaper()
                    videoLiveWallpaper?.setToWallPaper(this)
                }
            }
        }
    }
}
